#encoding:utf-8
"""
views：处理移动客户端和服务器的交互工作
author： ztj
data： 2012-8-6
"""
from django.http import HttpResponse
from django.utils import simplejson
from szdb.models import User, Activity, News
from szdb.adjtime import getDatetime
from django.core import serializers
import traceback
###################################
##处理移动客户端用户登录功能
###################################
    
def login(request):
    user = User()
    flag = False
    try:
        usr = request.POST['username']
        psw = request.POST['password']
        [username,uid, flag] = user.login({'username':usr, 'password':psw})
    except Exception,e :
        traceback.print_exc()
        flag = False;
    #登录成功
    finally:
        if flag:
            return HttpResponse(simplejson.dumps(uid))
        else:
            return HttpResponse(simplejson.dumps(flag))


###################################
##处理移动客户端客户的用户注册功能
###################################    
def register(request):
    flag = False
    user = User()
    try:
        usr = request.POST['username']
        psw = request.POST['password']
        email = request.POST['email']
        sq = request.POST['safequestion']
        sa = request.POST['safeanswer']
        sex = request.POST['sex']
        [username,uid, flag] = user.register({'username':usr, 
                              'password':psw, 
                              'email':email, 
                              'safequestion':sq,
                              'safeanswer': sa, 
                              'sex':sex})
    except Exception, e:
        flag = False;
        traceback.print_exc()
    finally:
        if flag == False:
            return HttpResponse(simplejson.dumps(flag))
        else:
            return HttpResponse(simplejson.dumps(uid))


###################################
##处理移动客户端用户检索新闻的功能
###################################
def retrieve_news(request):
    #typeid = 1
    #lasttime = getDatetime('2012-07-17 15:02:15');
    news = News()
    json_serializer =  serializers.get_serializer("json")()
    response = HttpResponse()
    try:
        lasttime = request.POST['lasttime']
        typeid = request.POST['typeid']
        news = News()
        data = news.retrieve_news({'lasttime':getDatetime(lasttime), 'typeid':typeid})
        json_serializer.serialize(data, ensure_ascii=False,stream=response)
        return response
    except Exception, e:
        data = False
        traceback.print_exc()
        return HttpResponse(simplejson.dumps(data))
        
###################################
##处理移动客户端用户检索活动的功能
###################################
def retrieve_activity(request):
    acti = Activity()
    json_serializer =  serializers.get_serializer("json")()
    response = HttpResponse()
    try:
        lasttime = request.POST['lasttime']
        data = acti.retrieve_activity({'lasttime':getDatetime(lasttime)})
        json_serializer.serialize(data, ensure_ascii=False,stream=response)
        return response
    except:
        data = False
        traceback.print_exc()
        return HttpResponse(simplejson.dumps(data))

###################################
##处理移动客户端用户按照id去检索活动参与者列表的功能
###################################

def get_activity_memberlist(request):
    acti = Activity()
    json_serializer =  serializers.get_serializer("json")()
    response = HttpResponse()
    try:
        acti_id = request.POST['id']
        data = acti.get_activity_detail({'id':acti_id})
        json_serializer.serialize(data, ensure_ascii=False,stream=response,fields=())
        return response
    except:
        data = False
        traceback.print_exc()
        return HttpResponse(simplejson.dumps(data))


        
    
###################################
##处理移动客户端新建经理人圈活动的功能
###################################  
def join_activity(request):
    user = User()
    try:
        username = request.POST['username']
        activityid = request.POST['activityid']
        data = user.attend_activity({'username':username, 'activityid':activityid})
    except:
        data = False
        traceback.print_exc()
    finally:
        return HttpResponse(simplejson.dumps(data))
    
###################################
##处理移动客户端参加经理人圈活动的功能
###################################  
def add_activity(request):
    acti= Activity()
    try:
        title = request.POST['title']
        content = request.POST['content']
        releasetime = request.POST['releasetime']
        date = request.POST['date']
        place = request.POST['place']
        uid = request.POST['uid']
        print date
        data = acti.add_activity({'title': title,
                                      'date': getDatetime(date),
                                      'content': content,
                                      'place':place,
                                      'uid':uid})
    except:
        data = False
        traceback.print_exc()
    finally:
        print data
        return HttpResponse(simplejson.dumps(data))       

###################################
##修改密码
###################################  
def reset_key(request):
    user = User()
    try:
        psw = request.POST['password']
        usr = request.POST['username']
        newPsw = request.POST['newpsw']
        newPsw2 = request.POST['newpsw2']
        if newPsw != newPsw :
            return False
        data = user.reset_key({'username': usr,
                        'password': psw,
                        'newpsw':newPsw})
    except: 
        data = False
        traceback.print_exc()
    finally:
        return HttpResponse(simplejson.dumps(data))      
        
        


####################################
###通过密保问题验证,取回密码
#####################################
def get_key(request):
    user = User()
    try:
        usr = request.POST['username']
        sq = request.POST['safequestion']
        sa = request.POST['safeanswer']
        psw = request.POST['password'] #设置的新密码
        data = user.is_answer_right({'username':usr, 
                                     'safequestion': sq,
                                     'safeanswer':sa,
                                     'newpassword':psw})
    except:
        data = False
        traceback.print_exc()
    finally:
        return HttpResponse(simplejson.dumps(data)) 

###################################
###测试用户名是否可用
###################################
def check_username(request):
    user = User()
    try:
        username = request.POST['username']
        data = user.is_username_unique({'username':username})
    except:
        data = False
        traceback.print_exc()
    finally:
        return HttpResponse(simplejson.dumps(data))
        

######################################
####测试邮箱是否可用
#########################################
def check_email(request):
    user = User()
    try:
        email = request.POST['email']
        data = user.is_email_unique({'email':email})
    except:
        data = False
        traceback.print_exc()
    finally:
        return HttpResponse(simplejson.dumps(data))


        
        
    
        
    
    
    
    
    